// @author U099151W Sun Chenchen
// all functions in this file  are implemented by the author unless specifically indicated
var RenderingEngine = function() {
    var _NPCsCanvas;
    var _backgroundCanvas;
    var _debugMapCanvas;

    var _currentMap;
    var _player;

    function renderDebugGridBuilding() {
        var context = _backgroundCanvas.getContext("2d");
    }

    this.render = function() {
        // clear canvas
        _NPCsCanvas.getContext("2d").clearRect(0, 0, _NPCsCanvas.width, _NPCsCanvas.height);
        _backgroundCanvas.getContext("2d").clearRect(0, 0, _backgroundCanvas.width, _backgroundCanvas.height);
        _debugMapCanvas.getContext("2d").clearRect(0, 0, _debugMapCanvas.width, _debugMapCanvas.height);

        // render debug map view
        _currentMap.drawDebug(_debugMapCanvas, 0, 0, _debugMapCanvas.width, _debugMapCanvas.height);


        // render background and NPCs
        if (!_player.insideBuilding()) {
            var _currentGrids = _currentMap.getCurrentGrids();
            for (var i=0; i<_currentGrids.length; i++) {
                var currentGrid = _currentGrids[i];
                var index = currentGrid.getIndexOfGrid();
                var gridSize = currentGrid.getGridSize();
                var gridWidth = _backgroundCanvas.width / gridSize.col;
                var gridHeight = _backgroundCanvas.height / gridSize.row;
                var startX = index.colIndex * _backgroundCanvas.width - camera.getCameraParameter().sx * gridWidth;
                var startY = index.rowIndex * _backgroundCanvas.height - camera.getCameraParameter().sy * gridHeight;
                //currentGrid.drawDebugBackground(_backgroundCanvas, startX, startY, _backgroundCanvas.width, _backgroundCanvas.height);

                if (debugMode) {
                    currentGrid.drawDebugBackground(_backgroundCanvas, startX, startY, _backgroundCanvas.width, _backgroundCanvas.height);
                    currentGrid.drawDebugNPCs(_NPCsCanvas, startX, startY, _NPCsCanvas.width, _NPCsCanvas.height);
                } else {
                    currentGrid.drawBackground(_backgroundCanvas, startX, startY, _backgroundCanvas.width, _backgroundCanvas.height);
                    currentGrid.drawNPCs(_NPCsCanvas, startX, startY, _NPCsCanvas.width, _NPCsCanvas.height);
                }

            }
        } else {
            // draw outside the building
            if (debugMode) {
                _player.getBuilding().drawDebugNPCs(_NPCsCanvas, 0, 0, _NPCsCanvas.width, _NPCsCanvas.height);
                _player.getBuilding().drawDebugBackground(_backgroundCanvas, 0, 0, _backgroundCanvas.width, _backgroundCanvas.height);
            } else {
                _player.getBuilding().drawNPCs(_NPCsCanvas, 0, 0, _NPCsCanvas.width, _NPCsCanvas.height);
                _player.getBuilding().drawBackground(_backgroundCanvas, 0, 0, _backgroundCanvas.width, _backgroundCanvas.height);
            }
            //_player.getBuilding().drawDebugBackground(_backgroundCanvas, 0, 0, _backgroundCanvas.width, _backgroundCanvas.height);
        }

    };

    this.init = function(args) {
        _NPCsCanvas = document.getElementById(args.NPC_canvas);
        _backgroundCanvas = document.getElementById(args.background_canvas);
        _debugMapCanvas = document.getElementById(args.debug_map_canvas);

        _player = args.player;
        _currentMap = args.map;
    };

};
var renderingEngine = new RenderingEngine();